package com.lot.controller;

import com.lot.dao.LotAuthorityDao;
import com.lot.service.*;
import com.lot.util.Msg;
import com.lot.util.RedisUtil;
import com.lot.vo.lotUser.LotUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author zel
 * @Date 2020-05-21
 */
@Api(value = "demo测试")
@RestController
@RequestMapping("/demo")
public class TestController {
    @Autowired
    TestInterface testInterface;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private LotUserService lotUserService;
    @Autowired
    private LotRoleService lotRoleService;
    @Autowired
    private LotAuthorityService lotAuthorityService;
    @Autowired
    private LotAuthorityDao lotAuthorityDao;

    @GetMapping("test")
    public String test() {
        return "hello Springboot!";
    }

    @GetMapping("test2")
    public String test2() {
        return testInterface.demoTest();
    }

    @GetMapping("setstring")
    public Msg tests(@RequestParam String key, @RequestParam String velue) {
        redisUtil.set(key, velue, 60);
        return Msg.success().add(key, velue);
    }

    @GetMapping("getstring")
    public Msg tests(@RequestParam String key) {
        return Msg.success().add(key, redisUtil.get(key));
    }

    @PostMapping("setobject")
    public Msg testSet(@RequestBody LotUser user) {
        redisUtil.set(user.getUserName(), user, 60);
        return Msg.success().add(user.getUserName(), user);
    }

    @GetMapping("getobject")
    public Msg testGet(@RequestParam String key) {
        return Msg.success().add(key, redisUtil.get(key));
    }

//    @PostMapping("userlist")
//    @ApiOperation("获取用户列表")
//    public Msg test4() {
//        return Msg.success().add("userList", lotUserService.findList());
//    }

//    @PostMapping("rolelist")
//    @ApiOperation("获取角色列表")
//    public Msg test9() {
//        return Msg.success().add("RoleList", lotRoleService.findList());
//    }

    @PostMapping("roleslist")
    @ApiOperation("根据当前用户获取角色列表")
    public Msg tests() {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        return Msg.success().add("RoleList", simpleAuthorizationInfo.getRoles());
    }

    @PostMapping("authorityslist")
    @ApiOperation("根据当前用户获取权限列表")
    public Msg testt() {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        return Msg.success().add("AuthorityList", simpleAuthorizationInfo.getStringPermissions());
    }


//    @PostMapping("authoritylist")
//    @ApiOperation("获取权限列表")
//    @RequiresPermissions({"public"})
//    public Msg test8() {
//        return Msg.success().add("AuthorityList", lotAuthorityService.findList());
//    }

    @PostMapping("getrole")
    @ApiOperation("获取单个用户的角色")
    public Msg test7(@RequestParam String username) {
        return Msg.success().add("AuthorityList", lotRoleService.getRoleByAccount(username));
    }

    @PostMapping("getauthority")
    @ApiOperation("获取单个用户的权限")
    public Msg test6(@RequestParam String username) {
        return Msg.success().add("AuthorityList", lotAuthorityService.getAuthority(username));
    }

    @PostMapping("isauthorityurl")
    @ApiOperation("权限里面是否有这个Url地址")
    public Msg isAuthorityUrl(@RequestParam String url) {
        return Msg.success().add("UrlList", lotAuthorityService.isAuthorityUrl(url));
    }

}

